import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:village_android/component/Jd_Text.dart';
import 'package:village_android/component/jd_button.dart';
import 'package:village_android/utils/common_utils.dart';
import 'dart:async';

import 'package:village_android/utils/http_utlis.dart';


class RegisterSecondPage extends StatefulWidget {
  Map arguments;
  RegisterSecondPage({Key key, this.arguments}) : super(key: key);

  _RegisterSecondPageState createState() => _RegisterSecondPageState();
}

class _RegisterSecondPageState extends State<RegisterSecondPage> {
  String tel;
  bool sendCodeBtn = false;
  int seconds = 10;
  String code;
  @override
  void initState() {
    super.initState();
    this.tel = widget.arguments["tel"];
    this._showTimer();
  }

  //
  _showTimer() {
    print("执行倒计时");
    print(this.sendCodeBtn);
    Timer t;
    t = Timer.periodic(Duration(microseconds: 1000), (timer) {
      setState(() {
        this.seconds--;
      });
      if (this.seconds == 0) {
        t.cancel(); //清除定时器：
        setState(() {
          this.sendCodeBtn = true;
        });
      }
    });
  }

  //重新发送验证码：
  sendCode() async {
    setState(() {
      this.sendCodeBtn = false;
      this.seconds = 10;
      this._showTimer();
    });
    HttpUtils.post("/api/sendCode", data: {"phone": this.tel},
        success: (data) {
          CommonUtils.showToast(context, "验证码已发送");

        }, error: (errorMsg) {
          print(errorMsg);
        });
  }

  validateCode() async {

    HttpUtils.post("/api/validateCode", data: {"phone": this.tel, "code": this.code},
        success: (data) {
          CommonUtils.showToast(context, "验证码已发送");
          Navigator.pushNamed(context, '/RegisterThird');

        }, error: (errorMsg) {
          print(errorMsg);
        });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('用户注册-完成注册'),
      ),
      body: Container(
        padding: EdgeInsets.all(20),
        child: ListView(
          children: <Widget>[
            Container(
              margin: EdgeInsets.all(20),
              child: Text("请输入${this.tel}手机收到的验证码"),
            ),
            Stack(
              children: <Widget>[
                JdText(
                  text: "请输入验证码",
                  // password: true,
                  onChanged: (value) {
                    print(value);
                    this.code = value;
                  },
                ),
                Positioned(
                  right: 0,
                  top: 0,
                  child: this.sendCodeBtn? RaisedButton(
                    child: Text("重新${this.sendCodeBtn}发送"),
                    onPressed: this.sendCode,
                  ): RaisedButton(
                    child: Text('${this.seconds}秒后重发'),
                    onPressed: () {},
                  ),
                )
              ],
            ),
            SizedBox(height: 20),
            JdButton(
              text: "下一步",
              color: Colors.red,
            )
          ],
        ),
      ),
    );
  }
}